package org.brownflat.lighthouse.monitoring.ci;

import org.apache.log4j.Logger;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Feed {
    private List<Build> items = new ArrayList<Build>();
    private static final Logger LOG = Logger.getLogger(Feed.class);

    public void addBuild(Build build) {
        this.items.add(build);
        Collections.sort(items, new BuildComparator(new AscendingDateComparator()));
    }

    public boolean isCurrentlyBroken() {
        Map<String, Build> history = buildLatestBuildHistory();
        for (Build build : history.values()) {
            if(build.isBroken()) {
                return true;
            }
        }
        return false;
    }

    public Map<String, Build> buildLatestBuildHistory() {
        Map<String, Build> history = new HashMap<String, Build>();
        for (Build build : items) {
            history.put(build.getBuildSeriesKey(), build);
        }
        return history;
    }

    public List<Build> getCurrentlyBrokenBuilds() {
        Map<String, Build> history = buildLatestBuildHistory();
        List<Build> builds = new ArrayList<Build>();

        for(Build build : history.values()) {
            if(build.isBroken()) {
                builds.add(build);
            }
        }
        return builds;
    }

    public List<Build> getBuilds() {
        return items;
    }
}
